<?
require_once LIBS."ReversedProxy.class.php";

define("BLOGSPOT_BLOG_URL","http://hellohtml5.blogspot.com");

function process_blog_content($response) {

	// 转换favicon地址
	$favico_pattern = "/<link[^>]*rel='icon'[^>]*>/";
	$favico_replace = "<!--[if IE]><link href='http://cdn.doctype.info/hellohtml5/favicon.ico' rel='icon' type='image/vnd.microsoft.icon'/><![endif]-->\n<link href='http://cdn.doctype.info/hellohtml5/favicon.png' rel='icon type='image/png''/>";
	$response = preg_replace($favico_pattern,$favico_replace,$response);

	// 移除blogger导航条
	$navbar_pattern = "/<div class='navbar .*?><div class='widget Navbar' .*?><script type=.*?>.*?<\/script>.*?<iframe src=\".*?><\/iframe>.*?<div>(<\/div>){3}/s";
    $response = preg_replace($navbar_pattern,"\n",$response);

	// 移除quickedit
	$quick_edit = "@<span class=\'widget-item-control\'>([^>]*>){6}@si";
    $response = preg_replace($quick_edit,"\n",$response);

	// 某些被屏蔽的JS文件使用CDN镜像
	$response = str_replace("http://www.blogger.com/static/v1/widgets/1947642054-widgets.js","http://cdn.doctype.info/blogger/211018530-widgets.js",$response);

	// 替换所有hellohtml5.blogspot.com域为当前域
	// 给所有www.blogger.com下的超链接做代理并加盐处理
    $banned_url = array(
        "@http:\/\/hellohtml5\.blogspot\.com@i",
		"@(<a[^>]*comment\-link[^>]*)(https?:\/\/www\.blogger\.com)@i", // 评论链接
        "@href\s*=\s*([\"\'])https?:\/\/www\.blogger\.com@i", // 普通链接
    );
    $replaced_url = array(
        "http://".SERVER_HOST,
		"$1http://".SERVER_HOST."/".MAGIC_STR."comment/http://www.blog!ger.com",
        "href=$1http://".SERVER_HOST."/".MAGIC_STR."http://www.blog!ger.com",// 给主域名加盐，以防止触发关键字屏蔽
    );
    $response = preg_replace($banned_url,$replaced_url,$response);

	//移除godaddy广告
    if($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "zh-CN"){
        $response .= "<script>"; 
	}

	return $response;
}
/**
 */
function blogspot($blog_req_path) {
	// path路径格式化
	// 没有以“/”开头或有多个“/”替换为一个“/”
	$blog_req_path = preg_replace("@^/*@","/",$blog_req_path);
	$blog_req_url = BLOGSPOT_BLOG_URL.$blog_req_path;
	$client = new ReversedProxy($blog_req_url);
	//$client->setopt(RVSPOPT_USE_SOCKET_HTTP,true);
	$client->exec_proxy("process_blog_content");
}
